package com.vvlive.caibao.smartcity.felm;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.vvlive.caibao.smartcity.bean.LifeOrderMealEatCateBean;

import java.util.List;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * listview二级联动(左右)Adapter
 * */
public class TestSectionedAdapter extends SectionedBaseAdapter {
	
	private Context mContext;
	private String[] leftStr;
	private String[][] rightStr;
	public TestSectionedAdapter(Context context, String[] leftStr, String[][] rightStr){
		this.mContext = context;
		this.leftStr = leftStr;
		this.rightStr = rightStr;
	}
    /**↓↓↓布局*/
    List<LifeOrderMealEatCateBean> mList;
    //用于已选的美食列表
    List<LifeOrderMealEatCateBean> mYiXuanBeanList;
    List<TextView> viewList;
    //选择的总数
    int zNum = 0;
    //单个美食选项的数目
    int[][] tag;
    double peisong;//配送起价
    double zongjia = 0;//已选总价
    public TestSectionedAdapter(Context context, String[] leftStr, String[][] rightStr, List<LifeOrderMealEatCateBean> mList, List<LifeOrderMealEatCateBean> mYiXuanBeanList, List<TextView> viewList, double peisong){
        this.mContext = context;
        this.leftStr = leftStr;
        this.rightStr = rightStr;

        this.mList = mList;
        this.viewList = viewList;
        this.peisong = peisong;
        this.mYiXuanBeanList = mYiXuanBeanList;
        tag = new int[leftStr.length][mList.size()];
    }
    /**
    public TestSectionedAdapter(Context context, List<LifeOrderMealEatCateBean> mList, List<LifeOrderMealEatCateBean> mYiXuanBeanList, List<TextView> viewList, double peisong) {
        mContext = context;
        this.mList = mList;
        this.viewList = viewList;
        this.peisong = peisong;
        this.mYiXuanBeanList = mYiXuanBeanList;
        tag = new int[mList.size()];
    }
    /**↑↑↑*/
    @Override
    public Object getItem(int section, int position) {
        return rightStr[section][position];
    }

    @Override
    public long getItemId(int section, int position) {
        return position;
    }

    @Override
    public int getSectionCount() {
        return leftStr.length;
    }

    @Override
    public int getCountForSection(int section) {
        return rightStr[section].length;
    }

    @Override
    public View getItemView(final int section, final int position, View convertView, ViewGroup parent) {
//        LinearLayout layout = null;
//        if (convertView == null) {
//            LayoutInflater inflator = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//            layout = (LinearLayout) inflator.inflate(R.layout.life_felm_list_item, null);
//        } else {
//            layout = (LinearLayout) convertView;
//        }
//        ((TextView) layout.findViewById(R.id.textItem)).setText(rightStr[section][position]);
//        layout.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View arg0) {
//				Toast.makeText(mContext, rightStr[section][position], Toast.LENGTH_SHORT).show();
//			}
//		});
//        return layout;
        final ViewHolder vh;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.life_item_order_meal_eat_list,null);
            vh = new ViewHolder();
            //            vh.mIcon = (ImageView) convertView.findViewById(R.id.order_meal_ls_item_icn);
            vh.mName = (TextView) convertView.findViewById(R.id.order_meal_lsitem_eat_name);
            vh.mManjian = (TextView) convertView.findViewById(R.id.order_meal_lsitem_eat_manjian);
            vh.mYshou = (TextView) convertView.findViewById(R.id.order_meal_lsitem_yueshou);
            vh.mPjNum = (TextView) convertView.findViewById(R.id.order_meal_lsitem_jiping);
            vh.mPj = (RatingBar) convertView.findViewById(R.id.order_meal_lsitem_eat_str);
            vh.price = (TextView) convertView.findViewById(R.id.order_meal_lsitem_jg);
            vh.jia = (ImageView) convertView.findViewById(R.id.order_meal_lsitem_jia);
            vh.item_k_num = (TextView) convertView.findViewById(R.id.order_meal_lsitem_num);
            vh.jian = (ImageView) convertView.findViewById(R.id.order_meal_lsitem_jian);
            convertView.setTag(vh);
        }else{
            vh = (ViewHolder) convertView.getTag();
        }
        Log.i("没事列表", "position===" + position + "      tag===" + tag[section][position]);
        //解决刷新时缓存问题
        if(tag[section][position]==0){
            vh.jian.setVisibility(View.GONE);
            vh.item_k_num.setVisibility(View.GONE);
        }else{
            vh.jian.setVisibility(View.VISIBLE);
            vh.item_k_num.setVisibility(View.VISIBLE);
        }

        //触发添加按钮
        vh.jia.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
//                Toast.makeText(mContext,"postion"+position,Toast.LENGTH_SHORT).show();
                tag[section][position]++;
                zNum++;
                vh.jian.setVisibility(View.VISIBLE);
                vh.item_k_num.setVisibility(View.VISIBLE);
                zongjia+= mList.get(position).getPrice();
                mYiXuanBeanList.add(mList.get(position));
                vh.item_k_num.setText(tag[section][position]+"");
                refreshButtom();
            }
        });
        //触发减按钮
        vh.jian.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                tag[section][position]--;
                zNum--;
                if (tag[section][position] > 0) {
                    zongjia -= mList.get(position).getPrice();
                    mYiXuanBeanList.remove(mList.get(position));
                } else {
                    vh.jian.setVisibility(View.GONE);
                    vh.item_k_num.setVisibility(View.GONE);
                }
                vh.item_k_num.setText(tag[section][position]+"");
                refreshButtom();
            }
        });
        vh.item_k_num.setText(tag[section][position]+"");
        //        vh.mIcon;
        vh.mName.setText(mList.get(position).getName());
        vh.mManjian.setText(mList.get(position).getManjian());
        vh.mYshou.setText(" 月售"+mList.get(position).getyNum());
        vh.mPjNum.setText(" "+mList.get(position).getpNum()+"评价");
        vh.mPj.setProgress(mList.get(position).getPingxing());
        vh.price.setText("￥"+mList.get(position).getPrice()+"");
        return convertView;
    }

    @Override
    public View getSectionHeaderView(int section, View convertView, ViewGroup parent) {
        LinearLayout layout = null;
        if (convertView == null) {
            LayoutInflater inflator = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layout = (LinearLayout) inflator.inflate(R.layout.life_felm_header_item, null);
        } else {
            layout = (LinearLayout) convertView;
        }
        layout.setClickable(false);
        ((TextView) layout.findViewById(R.id.textItem)).setText(leftStr[section]);
        return layout;
    }




    /**
     * 刷新按钮状态
     * */
    public void refreshButtom(){
        if(zNum>0){
            viewList.get(0).setText(zNum+"份 ");
            viewList.get(1).setText(" ￥"+zongjia);
            if((peisong-zongjia)>0){
                viewList.get(2).setText("派送还需 ￥"+(peisong-zongjia));
            }else{
                viewList.get(2).setText("");
            }
        }else{
            viewList.get(0).setText("");
            viewList.get(1).setText("");
            viewList.get(2).setText("");
        }

    }
    class ViewHolder{
        //加减图标
        ImageView jia;
        TextView item_k_num;
        ImageView jian;
        ImageView mIcon;
        TextView mName;     //名字
        TextView mManjian;  //满减
        TextView mYshou;    //月售
        TextView mPjNum;    //评价条数
        RatingBar mPj;      //评星
        TextView price;     //价格
    }
}
